import {Component} from '@angular/core';
import { Storage } from '@ionic/storage';

import {
  ViewController
} from 'ionic-angular';

import { Common } from '../../../../providers/common';

import _ from 'lodash';
import {MarketingDailyCollectService} from "./collect.service";

@Component({
  selector: 'page-marketing-daily-collect',
  templateUrl: './collect.html',
  providers: [MarketingDailyCollectService]
})
export class MarketingDailyCollectPage {
	moa_page_name: any = "page-marketing-daily-collect";
  moa_page_explain: any = "工作日报汇总";

  condition = null;

  defaultEmpByTree = [];

  //可以选择的最大日期
  maxDate = "";
  //结束日期可以选择的最小日期
  minEndDate = "1970-01-01";

  selectDate = "";

  pageIndex = 1;

  collectContent = "";

  searchEnable: boolean = false;

  constructor(
              public viewCtrl: ViewController,
              public storage: Storage,
              public common: Common,
              public collectService: MarketingDailyCollectService
  ) {
      //可以选择的日期不能超过今天
      this.maxDate = this.common.getNowFormatDate();
      this.selectDate = this.common.getNowFormatDate();
  }

  ionViewDidLoad() {
    let collectContent = document.getElementById("collectContentDiv");
    let ionContent = document.getElementById("ionContent");
    let ionList = document.getElementById("ionList");
    let ionRow = document.getElementById("ionRow");
    collectContent.style.height = (1 - (ionList.offsetHeight + ionRow.offsetHeight) / ionContent.offsetHeight) * 100 - 3 + "%";
  }

  getCollectContent() {
    this.storage.get("userInfo").then(user => {
      this.collectService.getListData(this.pageIndex, user.emname, this.selectDate).subscribe(dataList => {
        console.log(dataList);
          if (dataList) {
            _.forEach(dataList, (data, i) => {
              console.log(data);
              let temp = (i + 1) + "、" + data["cusname"] + " " + data["TYPENAME"] + " " + data["REASON"] + "。";
              if (this.collectContent == "") {
                this.collectContent = temp;
              } else {
                this.collectContent += temp;
              }
            });
            this.collectContent = this.selectDate.replace("-", ".").replace("-", ".") + "日报：" + this.collectContent;
            this.pageIndex++;
            // this.getCollectContent();
          }
        this.searchEnable = false;
      });
    });
  }

  cancel() {
    this.viewCtrl.dismiss("close");
  }

  clear() {
    this.collectContent = "";
    this.selectDate = this.common.getNowFormatDate();
  }

  search() {
    if (this.searchEnable) {
      return;
    }
    this.searchEnable = true;
    this.collectContent = "";
    this.pageIndex = 1;
    this.getCollectContent();
  }

}


